<%--
  Created by IntelliJ IDEA.
  User: Mr.G
  Date: 2019/1/2
  Time: 10:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <%@include file="/WEB-INF/jsp/common.jsp"%>
</head>
<body>
<div class="layui-fluid">
<h2>权限管理</h2>
<hr>

<table id="perTable" lay-filter="perTable"></table>
</div>

<script>

    layui.config({
        base: '${path}/assets/layui/lay/modules/'
    }).extend({
        treeGrid : 'treeGrid'
    })
    var treeGrid;
    layui.use(['treeGrid','table','notice'],function () {
        treeGrid= layui.treeGrid;
        var table = layui.table;
        var notice = layui.notice;


        // // 全部参数都是可选参数
        // var notice = {
        //     type: "",
        //     calssName: "",
        //     title: "创建普通notice通知",
        //     icon: "",
        //     align: "",
        //     autoClose: true,
        //     time: 3000,
        //     click: true,
        //     desktop: true, // 开启桌面提醒
        //     end: function (){
        //
        //     }
        // };
        // layui.notice.init(notice);


        $.get("${path}/menuforon/list",function (d) {

            $.each(d.data,function () {
                this.isOpen = false;
            });


            treeGrid.render({
                elem:"#perTable",
                data:d.data,
                treeId : 'menuid',
                treeUpId : 'parentid',
                treeShowName : 'menuname',
                toolbar:"#toolbarDemo",
                cols:[[
                    {type:"checkbox"},
                    {field:"menuid",title:"ID",width:80},
                    {field:"menuname",title:"权限名称"},
                    {field:"menulevel",title:"类型",templet: function(d){
                            if (d.menulevel == 0){
                                return "一级标题"
                            } else if(d.menulevel == 1){
                                return "二级标题"
                            }else{
                                return "三级标题"
                            }
                        }},
                    {field:"oaaction",title:"链接地址"},
                    {field:"oaimgpath",title:"图标样式"},
                    {field:"option",title:"操作",templet:"#tool"}
                ]]
            })
        })

        //监听行内事件
        treeGrid.on('tool(perTable)', function(obj){
            var e = obj.event;
            var data = obj.data;
            if (e == "edit") {/*修改权限*/
                $.get("${path}/menuforon/menufotonfrom2",function (str) {
                    $.get("${path}/menuforon/getMenuFoton",{menuid:data.menuid},function (menuforn) {
                        console.log(menuforn)
                        layer.open({
                            type: 1,
                            title: "修改权限",
                            content: str,
                            area: ['800px'],
                            btn: ['确定', '取消'],
                            maxmin: true,
                            success: function () {
                                layui.form.val('per2Form', menuforn);
                            },
                            yes:function (index) {
                                var parans = $("#menuforon").serialize();
                                $.get("${paht}/menuforon/update",parans,function (da) {
                                    // treegrid 重载数据
                                    $.get("${path}/menuforon/list",function (d) {

                                        $.each(d.data,function () {
                                            this.isOpen = false;
                                        });


                                        treeGrid.render({
                                            elem:"#perTable",
                                            data:d.data,
                                            treeId : 'menuid',
                                            treeUpId : 'parentid',
                                            treeShowName : 'menuname',
                                            toolbar:"#toolbarDemo",
                                            cols:[[
                                                {type:"checkbox"},
                                                {field:"menuid",title:"ID",width:80},
                                                {field:"menuname",title:"权限名称"},
                                                {field:"menulevel",title:"类型",templet: function(d){
                                                        if (d.menulevel == 0){
                                                            return "一级标题"
                                                        } else if(d.menulevel == 1){
                                                            return "二级标题"
                                                        }else{
                                                            return "三级标题"
                                                        }
                                                    }},
                                                {field:"oaaction",title:"链接地址"},
                                                {field:"oaimgpath",title:"图标路径"},
                                                {field:"option",title:"操作",templet:"#tool"}
                                            ]]
                                        })
                                    })
                                    layer.close(index);
                                    layer.msg("修改成功！",{icon:6})
                                })
                            }
                        })
                    })
                })

            }else if(e =="del"){/*单个删除*/
                $.get("${path}/menuforon/isErJi",{menuid:data.menuid},function (str) {
                    if (str.code ==1){
                        layer.msg('不能删除，因为存在二级标题', {icon: 5})
                    }else{
                        layer.confirm('确定要删除我吗？', {icon: 3, title:'友情提示'}, function(index){
                            $.post("${apth}/menuforon/del" ,{menuid:data.menuid},function (str) {
                                // treegrid 重载数据
                                $.get("${path}/menuforon/list",function (d) {

                                    $.each(d.data,function () {
                                        this.isOpen = false;
                                    });


                                    treeGrid.render({
                                        elem:"#perTable",
                                        data:d.data,
                                        treeId : 'menuid',
                                        treeUpId : 'parentid',
                                        treeShowName : 'menuname',
                                        toolbar:"#toolbarDemo",
                                        cols:[[
                                            {type:"checkbox"},
                                            {field:"menuid",title:"ID",width:80},
                                            {field:"menuname",title:"权限名称"},
                                            {field:"menulevel",title:"类型",templet: function(d){
                                                    if (d.menulevel == 0){
                                                        return "一级标题"
                                                    } else if(d.menulevel == 1){
                                                        return "二级标题"
                                                    }else{
                                                        return "三级标题"
                                                    }
                                                }},
                                            {field:"oaaction",title:"链接地址"},
                                            {field:"oaimgpath",title:"图标路径"},
                                            {field:"option",title:"操作",templet:"#tool"}
                                        ]]
                                    })
                                })
                                layer.close(index);
                                notice.init({
                                    type: "warm",
                                    autoClose: true,
                                    title: "删除成功！"
                                });
                            })
                        });
                    }
                })
            }


        })
    })
    /*添加权限*/
    function tinajia(){
        $.get("${path}/menuforon/menufotonfrom",function (str) {
            layer.open({
                type: 1,
                title:"添加权限",
                content: str,
                area: ['800px'],
                btn:['确定','取消'],
                maxmin: true,
                yes:function (index) {
                    var parems = $("#menuforon").serialize();
                    console.log(parems)
                    $.get("${path}/menuforon/add",parems,function (flag) {
                        //刷新
                        treeGrid.reload("perTable",{
                            page:{
                                curr:1
                            }
                        })
                        // treegrid 重载数据
                        $.get("${path}/menuforon/list",function (d) {

                            $.each(d.data,function () {
                                this.isOpen = false;
                            });


                            treeGrid.render({
                                elem:"#perTable",
                                data:d.data,
                                treeId : 'menuid',
                                treeUpId : 'parentid',
                                treeShowName : 'menuname',
                                toolbar:"#toolbarDemo",
                                cols:[[
                                    {type:"checkbox"},
                                    {field:"menuid",title:"ID",width:80},
                                    {field:"menuname",title:"权限名称"},
                                    {field:"menulevel",title:"类型",templet: function(d){
                                            if (d.menulevel == 0){
                                                return "一级标题"
                                            } else if(d.menulevel == 1){
                                                return "二级标题"
                                            }else{
                                                return "三级标题"
                                            }
                                        }},
                                    {field:"oaaction",title:"链接地址"},
                                    {field:"oaimgpath",title:"图标路径"},
                                    {field:"option",title:"操作",templet:"#tool"}
                                ]]
                            })
                        })
                        layer.close(index)
                    })
                }
            });
        })
    }
</script>
<script type="text/html" id="toolbarDemo">
    <shiro:hasPermission name="per:add">
        <button type="button" class="layui-btn"  lay-event="add" onclick="tinajia()"><i class="layui-icon layui-icon-add-1"></i>添加权限</button>
    </shiro:hasPermission>
</script>

<script type="text/html" id="tool">
    <shiro:hasPermission name="per:upd">
        <a href="javascript:void(0)" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
    </shiro:hasPermission>
    <shiro:hasPermission name="per:del">
        <a href="javascript:void(0)" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
    </shiro:hasPermission>
</script>
</body>
</html>
